import { reqAddHospital, reqGetHospital, reqUpdateHospital } from '@/api/hospital/hospitalSet'
import { ReqAddHospitalParams } from '@/api/hospital/model/hospitalSetType'
import { Button, Card, Form, Input, message } from 'antd'
import { useForm } from 'antd/lib/form/Form'
import React, { useEffect, useState } from 'react'
import { useLocation, useNavigate, useParams } from 'react-router-dom'

function AddOrUpdateHospital() {
    let [loading, setLoading] = useState(false)
    let { id } = useParams()
    let navigate = useNavigate()
    let [form] = useForm()
    let back = function () {
        navigate(-1)
    }
    let onfinish = async function () {
        let values: ReqAddHospitalParams = form.getFieldsValue()
        setLoading(true)
        try {
            if (typeof id != "undefined") {
                await reqUpdateHospital({ ...values, id })
                message.success('修改成功')
            } else {
                await reqAddHospital(values)
                message.success('添加成功')
            }
            setLoading(false)
            navigate(-1)
        } catch (error) {
            console.log(error);
            setLoading(false)
        }
    }
    async function gethospital() {
        let relute = await reqGetHospital(id as string)
        form.setFieldsValue(relute)
    }
    useEffect(() => {
        gethospital()
    })
    return (
        <Card>
            <Form
                form={form}
                name="basic"
                labelCol={{ span: 2 }}
                wrapperCol={{ span: 22 }}
                initialValues={{ remember: true }}
                autoComplete="off"
                onFinish={onfinish}
            >
                <Form.Item
                    label="医院名称"
                    name="hosname"
                    rules={[{ required: true, message: '请输入你的医院名称' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="医院编号"
                    name="hoscode"
                    rules={[{ required: true, message: '请输入你的医院编号' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="api基础路径"
                    name="apiUrl"
                    rules={[{ required: true, message: '请输入你的api基础路径' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="联系人姓名"
                    name="contactsName"
                    rules={[{ required: true, message: '请输入你的联系人姓名' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="联系人手机"
                    name="contactsPhone"
                    rules={[{ required: true, message: '请输入你的联系人手机' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item wrapperCol={{ offset: 2, span: 16 }}>
                    <Button type="primary" htmlType="submit" loading={loading}>
                        保存
                    </Button>
                    <Button className='ml-20' type="default" htmlType='reset' onClick={back}>
                        返回
                    </Button>
                </Form.Item>
            </Form>
        </Card>
    )
}

export default AddOrUpdateHospital
